package editor;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Vector;
import com.jme.math.Vector3f;
import com.jme.scene.Node;

import logic.Coords2D;
import arena.ArenaGS;
import editor.Menu.OGGETTI;
import gameObjects.Vector3fCoords;
import gameObjects.Vector3fCoords.WALLDIRECTION;

public class Grid {
	public final static int WIDTH  = 128;
	public final static int HEIGHT = 128;
	private static OGGETTI[][] cells = new OGGETTI[WIDTH][HEIGHT];
	public static Vector<ObjContainer> objs;
	public static Vector<Coords2D>	startPoints;
	public static Vector3fCoords walls3D;
	
	public final static Color emptyColor     = Color.white;
	public final static Color wallColor      = Color.black;
	public final static Color pillColor      = Color.yellow;
	public final static Color rubyRedColor   = Color.red;
	public final static Color rubyGreenColor = Color.green;
	public final static Color rubyBlueColor  = Color.blue;
	public final static Color startColor     = Color.orange;
	
	public Grid()
	{
		objs        = new Vector<ObjContainer>();
		startPoints = new Vector<Coords2D>();
		walls3D     = new Vector3fCoords(); 
		
		for (int x=0;x<WIDTH;x++)
		{
			for(int y=0;y<HEIGHT;y++)
			{
				cells[x][y] = OGGETTI.EMPTY;
			}
		}
	}
	
	public static OGGETTI[][] getGrid()
	{
		return cells;
	}
	
	public static void setCell(int x, int y)
	{
		OGGETTI t = Menu.currObject;
		cells[x][y]=t;
	}
	
	public static void setCell(int x, int y,OGGETTI t)
	{
		cells[x][y]=t;
	}
	
	public static OGGETTI getCell(int x, int y)
	{
		return cells[x][y];
	}
	
	public static void clearGrid()
	{
		cells = new OGGETTI[WIDTH][HEIGHT];
		for(int x=0;x<WIDTH;x++)
			for(int y=0;y<HEIGHT;y++)
				cells[x][y]=OGGETTI.EMPTY;
	}
	
	/**
	 * carica la griglia nel vettore objContainer
	 * @author Massimiliano Palermo
	 */
	public static void loadGrid(String filePath) throws IOException
	{ 
		int x;
		int y;
		OGGETTI t;
		FileReader fileReader = new FileReader(filePath);
		BufferedReader buffer = new BufferedReader(fileReader); 
		String line=""; 
		String arr[];
		
		clearGrid();
		
		while(true)
		{
			line=buffer.readLine();
			if (line!=null)
			{
				arr=line.split(" ");
				x=Integer.parseInt(arr[0]);
				y=Integer.parseInt(arr[1]);
				t=OGGETTI.valueOf(arr[2]);
				//if (t==OGGETTI.EXTWALL) t=OGGETTI.WALL;
				Grid.setCell(x, y, t);
				
				switch(t)
				{
				case RUBYBLUE:	 objs.add(new ObjContainer(x,y,t));break;
				case RUBYGREEN:	 objs.add(new ObjContainer(x,y,t));break;
				case RUBYRED:	 objs.add(new ObjContainer(x,y,t));break;
				case PILL:		 objs.add(new ObjContainer(x,y,t));break;
				case STARTPOINT: startPoints.add(new Coords2D(x, y));break;
				}
			}
			else break;
		}
		
		
	}
	
	public static void exportWall(String filePath) throws IOException
	{
		OGGETTI[][] twin = new OGGETTI[Grid.WIDTH][Grid.HEIGHT];
		twin = Grid.getGrid();
		OGGETTI t;
		int X=0;
		int Y=0;
		
		File f=new File(filePath);
		FileOutputStream fos;
		fos = new FileOutputStream(f);
		PrintStream ps=new PrintStream(fos);
		
		for(int x=0;x<WIDTH;x++)
		{
			for(int y=0;y<HEIGHT;y++)
			{
				if(twin[x][y]==OGGETTI.EXTWALL)
				{
					t=OGGETTI.EXTWALL;
					X=x;
					Y=y;
					while(t==OGGETTI.EXTWALL)
					{
						twin[x][y] = OGGETTI.EMPTY;
						y++;
						t=twin[x][y];
					}
					if(y-Y==1)
						twin[x][Y]=OGGETTI.EXTWALL;
					else
						ps.println(X+" "+Y+" "+(y-Y)+" "+OGGETTI.WALL+" C");
				}
			}
		}
		
		for(int y=0;y<WIDTH;y++)
		{
			for(int x=0;x<HEIGHT;x++)
			{
				if(twin[x][y]==OGGETTI.EXTWALL)
				{
					t=OGGETTI.EXTWALL;
					X=x;
					Y=y;
					while(t==OGGETTI.EXTWALL)
					{
						twin[x][y] = OGGETTI.EMPTY;
						x++;
						t=twin[x][y];
					}
					twin[X][y]=OGGETTI.EXTWALL;
					ps.println(X+" "+Y+" "+(x-X)+" "+OGGETTI.WALL+" R");
				}
			}
		}
		fos.close();
	}
	
	public static void loadWalls(String levelName) throws IOException
	{ 
		int x,z,tot;
		String versus;
		WALLDIRECTION wallDirection = null;
		FileReader fileReader = new FileReader(levelName);
		BufferedReader buffer = new BufferedReader(fileReader); 
		String line=""; 
		String parts[];
		tot=0;
		walls3D = new Vector3fCoords();
		
		for(int xx=0;xx<WIDTH;xx++)
		{
			Grid.setCell(xx, 0, OGGETTI.EXTWALL);
			Grid.setCell(xx, HEIGHT-1, OGGETTI.EXTWALL);
		}
		for(int yy=0;yy<WIDTH;yy++)
		{
			Grid.setCell(0, yy, OGGETTI.EXTWALL);
			Grid.setCell(WIDTH-1, yy, OGGETTI.EXTWALL);
		}
		
		line=buffer.readLine();
		while(line!=null)
		{	
			parts	=	line.split(" ");
			x		=	Integer.parseInt(parts[0]);
			z		=	Integer.parseInt(parts[1]);
			tot		=	Integer.parseInt(parts[2]);
			versus	=	parts[4];	
			Vector3f startPoint = new Vector3f();
			Vector3f endPoint   = new Vector3f();
			
			if(versus.equals("R"))
			{ 	startPoint = new Vector3f(x,0,z);
				endPoint   = new Vector3f(x+tot,2,z+1);
				wallDirection = WALLDIRECTION.ROW;
			}
			else if(versus.equals("C"))
			{
				startPoint = new Vector3f(x,0,z);
				endPoint   = new Vector3f(x+1,2,z+tot);
				wallDirection = WALLDIRECTION.COLUMN;
			}
			System.out.println("THEWALL (" + versus + ") " + startPoint.toString() + ", " + endPoint.toString());
			walls3D.setCoord(startPoint,endPoint,wallDirection);

			line=buffer.readLine();
		}
	}
		
		public static void saveGrid(String filePath) throws IOException
		{
			File f=new File(filePath);
			FileOutputStream fos;
			OGGETTI t;
			fos = new FileOutputStream(f);
			PrintStream ps=new PrintStream(fos);
			for(int i=0;i<Grid.WIDTH;i++)
			{
				for(int j=0;j<Grid.HEIGHT;j++)
				{
					t=Grid.getCell(i, j);
					if(t!=OGGETTI.EMPTY)
					{
						if(t==OGGETTI.WALL)
							ps.println(i+" "+j+" "+OGGETTI.EXTWALL);
						else
							ps.println(i+" "+j+" "+Grid.getCell(i, j));
					}
				}
			}
			
			fos.close();
		}
		
		public static void remove(int x, int y)
		{
			String name = x + "x" + y;
			for (int i=0;i<objs.size();i++)
			{
				if (objs.get(i).getX()==x && objs.get(i).getY()==y)
				{
					objs.remove(i);
					ArenaGS.getInstance();
					ArenaGS.getInstance();
					System.out.println(ArenaGS.bonus.toString());
					Node n = (Node) ArenaGS.bonus.getChild(name);
					n.setLocalTranslation(new Vector3f(-10000,-10000,-10000));
				}	
			}
		}
}
